package com.nazca.tvremote.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.nazca.tvremote.R;
import com.nazca.tvremote.service.Controller;
import com.nazca.tvremote.view.TitleBar;

public class MainActivity extends Activity implements OnItemClickListener {
	
	private static final String TAG = "MainActivity";
	
	private static final int[] ICON = new int[]{
		R.drawable.ic_main_tv_remote, R.drawable.ic_main_gesture_remote, R.drawable.ic_main_keyboard,
		R.drawable.ic_main_game_remote, R.drawable.ic_main_manual, R.drawable.ic_main_settings,
		R.drawable.ic_main_motion_remote
	};
	private static final int[] TITLE = new int[]{
		R.string.main_tv_remote, R.string.main_gesture_remote, R.string.main_keyboard,
		R.string.main_game_remote, R.string.main_manual, R.string.main_settings,
		R.string.main_motion_remote
	};
	private static final Class<?>[] CLASS = new Class<?>[]{
		TVRemoteActivity.class, GestureRemoteActivity.class, KeyboardActivity.class,
		GameRemoteActivity.class, ManualActivity.class, SettingsActivity.class,
		MotionRemoteActivity.class
	};
	
	private TitleBar mTitleBar;
	private GridView mGridView;
	private GridAdapter mAdapter;
	
	private ControllerResults mControllerCallback = new ControllerResults();
	
	public static void actionMain(Context context){
		Intent intent = new Intent(context, MainActivity.class);
		context.startActivity(intent);
	}
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
//        Intent intent = getIntent();
//        boolean isExit = intent.getBooleanExtra("EXIT", false);
//        if(isExit){
//        	finish();
//        }
        
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.getBackButton().setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {
				finish();
			}
		});
		
        mGridView = (GridView) findViewById(R.id.grid);
        mGridView.setOnItemClickListener(this);
        
        mAdapter = new GridAdapter();
        mGridView.setAdapter(mAdapter);
        
    }
    
    @Override
	protected void onResume() {
		super.onResume();
		Controller.getInstance(getApplication()).addResultCallback(mControllerCallback);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Controller.getInstance(getApplication()).removeResultCallback(mControllerCallback);
	}
    
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    }
    
    private class ControllerResults implements Controller.Result{

		@Override
		public void connectToServer(boolean successful) {
			Log.d(TAG, "connectToServer()");
			Log.d(TAG, "successful:"+successful);
		}

		@Override
		public void disConnectToServer(boolean successful) {
			Log.d(TAG, "disConnectToServer()");
			Log.d(TAG, "successful:"+successful);
		}
	}
    
    private class GridAdapter extends BaseAdapter{
    	
		@Override
		public int getCount() {
			return TITLE.length;
		}

		@Override
		public Object getItem(int position) {
			return TITLE[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(convertView == null){
				convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.grid_item, null);
				
				holder = new ViewHolder();
				holder.iconView = (ImageView) convertView.findViewById(R.id.icon);
				holder.titleView = (TextView) convertView.findViewById(R.id.title);
				convertView.setTag(holder);
			} else{
				holder = (ViewHolder) convertView.getTag();
			}
			
			holder.iconView.setImageResource(ICON[position]);
			holder.titleView.setText(TITLE[position]);
			
			return convertView;
		}
		
		private class ViewHolder{
			public ImageView iconView;
			public TextView titleView;
		}
    }

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent(this, CLASS[position]);
		startActivity(intent);
	}
    
}